const formidable = require('formidable');
const path = require('path');
const { User } = require('../../model/user');
module.exports = async (req,res)=>{
  //1.创建表单解析对象
  const form = new formidable.IncomingForm();
  //2.配置文件上传的存放位置
  form.uploadDir = path.join(__dirname,'../','../','public','uploads');
  //3.保留文件上传的后缀
  form.keepExtensions = true;
  //4.解析表单
  form.parse(req,async (err,fields,files)=>{
    //1.err错误对象如果表单解析失败err里面存储错误信息如果表单解析成功err将会
    // 2.fields对象类型保存普通表单数据
    //3.files 对象类型保存了和上传文件相关的数据

    await User.findByIdAndUpdate(fields._id,{
      username: fields.username,
      email: fields.email,
      password: fields.password,
      role: fields.role,
      state: fields.state,
      introduce: fields.introduce,
      profilePhoto: files.profilePhoto.path.split('public')[1]
    });

    res.redirect('/admin/toUserList');
  });
  //res.send('ok');
}